package practice.tmp;

public class Solution41 {
    public static void main(String[] args) {
        int[] arr = new int[]{3,4,-1,1};
        int i = firstMissingPositive(arr);
        System.out.println(i);
    }
    public static int firstMissingPositive(int[] nums) {
        int len = nums.length;
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] > 0 && nums[i] <= len && nums[nums[i] - 1] != nums[i]) {
                int tmp = nums[i];
                nums[i] = nums[nums[i] - 1];
                nums[tmp - 1] = tmp;
            }
        }
        for (int i = 0; i < len; i++) {
            if (nums[i] != i + 1) return i + 1;
        }
        return len + 1;
    }
}
